import { request } from "../../request/index.js";
import regeneratorRuntime from "../../libs/runtime/runtime";

Page({
    /**
     * 页面的初始数据
     */
    data: {
        radio: "1", //默认是男
        date: "请选择", //默认日期
        iamges: "", //用户头像
        nickName: "", //用户姓名
        phone: "",
    },
    form: {
        sex: "1", //默认是男
        birthday: "", //默认日期
        phone: null, //手机号
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.userNameInfoPost(); //用户信息
    },
    //用户信息
    async userNameInfoPost() {
        let res = await request({ url: "User/findById" });
        console.log(res);
        if (res.code == 200) {
            this.setData({
                iamges: res.data.img,
                nickName: res.data.name,
                phone: res.data.phone || "请绑定",
                date: res.data.birthday || "请选择",
                radio: res.data.sex + "" || "1",
            });
        }
    },
    // 绑定事件
    onInput(event) {
        console.log(event);
        this.setData({
            currentDate: event.detail,
        });
    },
    // 点击生日触发事件
    bindDateChange(e) {
        console.log("picker发送选择改变，携带值为", e.detail.value);
        this.setData({
            date: e.detail.value,
        });
        this.form.birthday = e.detail.value;
    },
    // 性别按钮
    onChange(e) {
        console.log(e);
        this.setData({
            radio: e.detail,
        });
        this.form.sex = e.detail;
    },
    //更换手机号
    phoneBtn() {
        wx.navigateTo({
            url: "../mobile_phone/mobile_phone",
        });
    },
    // 退出登录按钮
    quitBtn() {
        wx.showModal({
            title: "是否退出登录?",
            content: "",
            showCancel: true,
            cancelText: "取消",
            cancelColor: "#000000",
            confirmText: "确定",
            confirmColor: "#3CC51F",
            success: (result) => {
                console.log(result);

                if (result.confirm) {
                    wx.switchTab({
                        url: "../myName/index",
                    });
                    wx.clearStorage();
                }
            },
            fail: () => {
                console.log("123");
            },
            complete: () => {},
        });
    },
    //保存按钮
    async saveBtn() {
        let res = await request({
            url: "User/update",
            data: this.form,
        });
        console.log(res);
        if (res.code == 200) {
            wx.showToast({
                title: "修改成功",
                duration: 2000,
            });
            wx.switchTab({
                url: "../myName/index",
            });
        } else {
            wx.showToast({
                title: res.msg,
                icon: "none",
                duration: 2000,
            });
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {},
});